package com.atguigu.dw.gmallpublisher.util

import com.atguigu.constan.ConstanVal
import io.searchbox.client.JestClientFactory
import io.searchbox.client.config.HttpClientConfig
import io.searchbox.core.{Search, SearchResult}

/**
 * description ：es 工具类
 * author      ：剧情再美终是戏 
 * mail        : 13286520398@163.com
 * date        ：Created in 2020/2/17 18:55
 * modified By ：
 * version:    : 1.0
 */
object EsUtil {

  // es 配置工厂
  val factory = new JestClientFactory()
  val httpClientConfig: HttpClientConfig = new HttpClientConfig
  .Builder(ConstanVal.ES_CLIENT_ADRESS)
    .maxTotalConnection(20)
    .multiThreaded(true)
    .connTimeout(1000 * 10)
    .readTimeout(1000 * 10)
    .build()
  factory.setHttpClientConfig(httpClientConfig)

  /**
   * es 查询数据工具类
   *
   * @Author 剧情再美终是戏
   * @Date 2020/2/20 20:37
   * @param index
   * @param searchDSL
   * @return io.searchbox.core.SearchResult
   * @Version 1.0
   **/
  def query(index: String, searchDSL: String): SearchResult = {
    // 构建查询 action
    val searchAction = new Search.Builder(searchDSL).addIndex(index).addType(ConstanVal.ES_DEFAULT_DOC).build()

    // 获取 es 连接并查询
    getEsClient().execute(searchAction)
  }

  /**
   * 获取 es 连接
   *
   * @Author 剧情再美终是戏
   * @Date 2020/2/17 19:0
   * @return io.searchbox.client.JestClient
   * @Version 1.0
   **/
  def getEsClient() = factory.getObject


}
